<template>
  <div style="padding: 10px">
    <el-form label-width="120px" style="margin-bottom: 10px">
      <el-row :gutter="20">
        <el-col :span="14">
          <el-form-item label="用户姓名">
            <el-input v-model="state.sd.searchName" placeholder="用户姓名"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button size="default" type="primary" class="ml10" @click="searchUser">
            <el-icon><ele-Search /></el-icon>查询
          </el-button>
          <el-button size="default" type="primary" class="ml10" @click="addUser">
            <el-icon><ele-CirclePlus /></el-icon>添加
          </el-button>
        </el-col>
      </el-row>
    </el-form>

    <el-table :data="state.userObj.list" border style="width: 100%">
      <el-table-column label="用户名" prop="username"></el-table-column>
      <el-table-column label="用户姓名" prop="name"></el-table-column>
      <el-table-column label="手机号" prop="phone"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button size="small" type="primary" @click="editUser(scope.row.id)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="mt15" background layout="total, prev, pager, next"
                   @current-change="loadUser" @size-change="loadUser" :total="state.userObj.total"
                   v-model:current-page="state.sd.pageNum" v-model:page-size="state.sd.pageSize">
    </el-pagination>

    <el-dialog :title="state.popupTitle" v-model="state.entityDetailVisible" :close-on-click-modal="false" width="800px">
      <UserDetail v-if="state.entityDetailVisible" v-model="state.entityDetailVisible" :entityId="state.popupEntityId" :orgId="props.orgId"></UserDetail>
    </el-dialog>
  </div>
</template>

<script setup name="userIndex">
import request from '/@/utils/request';
import { ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
import {reactive, ref, onMounted, onUnmounted, defineAsyncComponent, defineProps} from "vue";
// 引入组件
const UserDetail = defineAsyncComponent(() => import('/@/views/sys/user/userDetail.vue'));

let props = defineProps({
  orgId : {type : Number, default: 0}
})
const state = reactive({
  sd: {
    searchName : '',
    orgId: props.orgId,
    pageNum: 1,
    pageSize: 15
  },
  userObj: {},

  entityDetailVisible : false,
  popupTitle : '',
  popupEntityId : 0,
})

const searchUser = ()=>{
  state.sd.pageNum = 1;
  loadUser();
}
const loadUser = ()=>{
  if(props.orgId !== 0){
    request({url: '/sys/useradmin/pageorguser', data: state.sd}).then(res => {
      const { data } = res
      state.userObj = data
    })
  }else {
    request({url: '/sys/useradmin/pageuser', data: state.sd}).then(res => {
      const { data } = res
      state.userObj = data
    })
  }
}
const addUser = ()=>{
  state.popupEntityId = 0;
  state.popupTitle= "新增组织用户"
  state.entityDetailVisible= true
}
const editUser = (id)=>{
  state.popupEntityId = id;
  state.popupTitle= "修改组织用户"
  state.entityDetailVisible= true
}
onMounted(()=>{
  loadUser();
  mittBus.on("useradd", ()=>{
    state.sd.pageNum = 1;
    loadUser();
  })
  mittBus.on("useredit", ()=>{
    loadUser();
  })
})
onUnmounted(()=>{
  mittBus.off("useradd", ()=>{})
  mittBus.off("useredit",()=>{})
})
</script>
<style scoped>
</style>
